package com.entities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.SparseArray;

public class MatchDetailed extends Match {

	private static final String CLUSTER = "cluster";
	private static final String BARRACKS_STATUS_DIRE = "barracks_status_dire";
	private static final String BARRACKS_STATUS_RADIANT = "barracks_status_radiant";
	private static final String TAG_RADIANT_WIN = "radiant_win";
	private static final String TAG_TOWER_STATUS_RADIANT = "tower_status_radiant";
	private static final String TAG_DURATION = "duration";
	private static final String TAG_TOWER_STATUS_DIRE = "tower_status_dire";
	private static final String GAME_MODE = "game_mode";
	private static final String NEGATIVE_VOTES = "negative_votes";
	private static final String POSITIVE_VOTES = "positive_votes";
	private static final String LEAGUEID = "leagueid";
	private static final String HUMAN_PLAYERS = "human_players";
	private static final String FIRST_BLOOD_TIME = "first_blood_time";

	public static HashMap<Long, MatchDetailed> allMatchsDetailed = new HashMap<Long, MatchDetailed>();
	private boolean radiant_win;
	private long duration;
	private long tower_status_radiant;
	private long tower_status_dire;
	private long barracks_status_radiant;
	private long barracks_status_dire;
	private long cluster;
	private long first_blood_time;
	private int human_players;
	private int leagueid;
	private int positive_votes;
	private int negative_votes;
	private int game_mode;
	private String radiant_name;
	private String dire_name;
	protected SparseArray<PlayerDetailed> mapPlayers;
	private List<PlayerDetailed> playersDetailed;

	public String getRadiant_name() {
		return radiant_name;
	}
	public String getDire_name() {
		return dire_name;
	}
	
	public List<PlayerDetailed> getPlayersDetailed() {
		return playersDetailed;
	}

	public SparseArray<PlayerDetailed> getSparseArrayPlayers() {
		if(mapPlayers==null){
			mapPlayers = new SparseArray<PlayerDetailed>();
			for(PlayerDetailed p: playersDetailed){
				mapPlayers.put(p.getPlayer_slot(), p);
			}
		}
		return mapPlayers;
	}
	
	private boolean isRadiant(int slot){
		if(slot<5){
			return true;
		} else return false;
	}
	
	public PlayerDetailed getPlayerOfAccId(String accId){
		PlayerDetailed playerDetailed = null;
		for(PlayerDetailed pd:getPlayersDetailed()){
			if(pd.getAccount_id().equals(Long.valueOf(accId))){
				playerDetailed= pd;
				break;
			}
		}
		return playerDetailed;
	}
	
	public boolean playeroOfAccIdWon(String accId){
		for(Player p: players){
			if(p.getAccount_id().equals(Long.valueOf(accId))){
				if(isRadiant(p.getPlayer_slot())){
					if(isRadiant_win()) return true;
					 else return false;
				} else {
					if(isRadiant_win()) return false;
					else return true;
				}
			}
		}
		return false;
	}
	
	public boolean isRadiant_win() {
		
		return radiant_win;
	}

	public long getDuration() {
		
		return duration;
	}
	public long getTower_status_radiant() {
		
		return tower_status_radiant;
	}
	public long getTower_status_dire() {
		
		return tower_status_dire;
	}
	public long getBarracks_status_radiant() {
		
		return barracks_status_radiant;
	}
	public long getBarracks_status_dire() {
		
		return barracks_status_dire;
	}
	public long getCluster() {
		
		return cluster;
	}
	public long getFirst_blood_time() {
		
		return first_blood_time;
	}
	public int getHuman_players() {
		
		return human_players;
	}
	public int getLeagueid() {
		
		return leagueid;
	}
	public int getPositive_votes() {
		
		return positive_votes;
	}
	public int getNegative_votes() {
		
		return negative_votes;
	}
	public int getGame_mode() {
		
		return game_mode;
	}
	
	/*public MatchDetailed createFromJSON(JSONObject json){
		
	}*/
	
	
	public MatchDetailed(JSONObject json) throws JSONException{

		List<Player> players = new ArrayList<Player>();
		JSONArray playersJSONArray = json.getJSONArray(TAG_PLAYERS);
		for(int pc = 0; pc<playersJSONArray.length(); pc++){
			JSONObject pl = playersJSONArray.getJSONObject(pc);
			players.add(new Player(pl.getLong(Player.TAG_ACC_IC), pl.getInt(Player.TAG_PLAYER_SLOT), pl.getInt(Player.TAG_HERO_ID))) ;
		}
		
		long match_id = json.getLong(TAG_MATCH_ID);
		long match_seq_num = json.getLong(TAG_MATCH_SEQ_NUM);
		long start_time = json.getLong(TAG_START_TIME);
		int lobby_type = json.getInt(TAG_LOBBY_TYPE);
		this.match_id= match_id;
		this.match_seq_num = match_seq_num;
		this.start_time = start_time;
		this.lobby_type = lobby_type;
		this.players = players;
		this.radiant_win = json.getBoolean(TAG_RADIANT_WIN);
		this.duration = json.getLong(TAG_DURATION);
		this.tower_status_radiant = json.getInt(TAG_TOWER_STATUS_RADIANT);
		this.tower_status_dire = json.getInt(TAG_TOWER_STATUS_DIRE);
		this.barracks_status_radiant = json.getInt(BARRACKS_STATUS_RADIANT);
		this.barracks_status_dire = json.getInt(BARRACKS_STATUS_DIRE);
		this.cluster = json.getInt(CLUSTER);
		this.first_blood_time = json.getLong(FIRST_BLOOD_TIME);
		this.human_players = json.getInt(HUMAN_PLAYERS);
		this.leagueid = json.getInt(LEAGUEID);
		this.positive_votes = json.getInt(POSITIVE_VOTES);
		this.negative_votes = json.getInt(NEGATIVE_VOTES);
		this.game_mode= json.getInt(GAME_MODE);
		try {
			this.radiant_name = json.getString("radiant_name");
		} catch (JSONException e) {
		  this.radiant_name = "";
		}	
		try {
			this.dire_name = json.getString("dire_name");
		} catch (JSONException e) {
		  this.dire_name = "";
		}
		playersDetailed = new ArrayList<PlayerDetailed>();
		
		for(int pc = 0; pc<playersJSONArray.length(); pc++){
			JSONObject pl = playersJSONArray.getJSONObject(pc);
			playersDetailed.add(new PlayerDetailed(new Player(pl.getLong(Player.TAG_ACC_IC), pl.getInt(Player.TAG_PLAYER_SLOT), pl.getInt(Player.TAG_HERO_ID)), pl)) ;
		}
		allMatchsDetailed.put(this.match_id, this);
		
	}
	
	
	
/*	private MatchDetailed(Match match) {
		super(match.getMatch_id(), match.getMatch_seq_num(), match.getStart_time(), match.getLobby_type(), match.getPlayers());
		try {
			
			JSONObject json = new FetchMatchsDetails(String.valueOf(match.getMatch_id())).execute().get().getJSONObject(Constants.RESULT);
			this.radiant_win = json.getBoolean(TAG_RADIANT_WIN);
			this.duration = json.getLong(TAG_DURATION);
			this.tower_status_radiant = json.getInt(TAG_TOWER_STATUS_RADIANT);
			this.tower_status_dire = json.getInt(TAG_TOWER_STATUS_DIRE);
			this.barracks_status_radiant = json.getInt(BARRACKS_STATUS_RADIANT);
			this.barracks_status_dire = json.getInt(BARRACKS_STATUS_DIRE);
			this.cluster = json.getInt(CLUSTER);
			this.first_blood_time = json.getLong(FIRST_BLOOD_TIME);
			this.human_players = json.getInt(HUMAN_PLAYERS);
			this.leagueid = json.getInt(LEAGUEID);
			this.positive_votes = json.getInt(POSITIVE_VOTES);
			this.negative_votes = json.getInt(NEGATIVE_VOTES);
			this.game_mode= json.getInt(GAME_MODE);
		
			playersDetailed = new ArrayList<PlayerDetailed>();
			
			JSONArray playersJSONArray = json.getJSONArray(TAG_PLAYERS);
			for(int pc = 0; pc<playersJSONArray.length(); pc++){
				JSONObject pl = playersJSONArray.getJSONObject(pc);
				playersDetailed.add(new PlayerDetailed(new Player(pl.getLong(Player.TAG_ACC_IC), pl.getInt(Player.TAG_PLAYER_SLOT), pl.getInt(Player.TAG_HERO_ID)), pl)) ;
			}
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
*/

}
